package com.xiaojimao.gr_user.Service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xaiojimao.gr_common.CommonEnum.BizCommonEnum;
import com.xaiojimao.gr_common.Utils.AsserUtils;
import com.xaiojimao.gr_common.Utils.JwtUtils;
import com.xaiojimao.gr_common.dto.Result;
import com.xaiojimao.gr_common.Utils.SaltUtils;
import com.xaiojimao.gr_common.dto.RequestInfo;
import com.xaiojimao.gr_common.vo.req.PageReq;
import com.xaiojimao.gr_common.vo.resp.PageResp;
import com.xiaojimao.gr_user.ThreadLocal.UserThread;
import com.xiaojimao.gr_user.dao.UserDao;
import com.xiaojimao.gr_user.interceptor.UserInterceptor;
import com.xiaojimao.gr_user.pojo.User;
import com.xiaojimao.gr_user.Service.UserService;
import com.xiaojimao.gr_user.vo.req.UserPermission;
import com.xiaojimao.gr_user.vo.req.UserStatus;
import com.xiaojimao.gr_user.vo.req.UserUpdate;
import com.xiaojimao.gr_user.vo.resp.UserInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 32987
 * @description 针对表【user(用户表)】的数据库操作Service实现
 * @createDate 2023-11-23 18:07:16
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public Result userLogin(User user) {
        String userName = user.getUserName();
        User userInfo = userDao.getUser(userName);
        AsserUtils.isEmpty(userInfo, BizCommonEnum.USER_NOT_EXITS_ERROR);
        String salt = userInfo.getSalt();
        String password = userInfo.getPassword();
        String pwd = DigestUtils.md5DigestAsHex((user.getPassword() + salt).getBytes());
        AsserUtils.eq(password, pwd, BizCommonEnum.USER_PASSWORD_ERROR);
        HashMap<String, Object> map = new HashMap<>();
        map.put("userId", userInfo.getId());
        String token = JwtUtils.getToken(map);
        return Result.success(token);
    }

    @Override
    public Result userRegister(User user) {
        String userName = user.getUserName();
        User userInfo = userDao.getUser(userName);
        AsserUtils.isNotEmpty(userInfo, BizCommonEnum.USER_EXITS_ERROR);
        String slat = SaltUtils.generateSalt();
        user.setSalt(slat);
        String pwd = DigestUtils.md5DigestAsHex((user.getPassword() + slat).getBytes());
        user.setPassword(pwd);
        String uuid_nickName = "xjm_" + UUID.randomUUID().toString().substring(0, 4);
        user.setNickName(uuid_nickName);
        user.setUpdateTime(LocalDateTime.now());
        userDao.save(user);
        return Result.success();
    }

    @Override
    public Result update(UserUpdate user) {
        User userInfo = new User();
        BeanUtils.copyProperties(user, userInfo);
        RequestInfo requestInfo = UserThread.get();
        String userId = requestInfo.getUserId();
        userInfo.setId(userId);
        userDao.updateById(userInfo);
        return Result.success();
    }

    @Override
    public Result userInfo() {
        String userId = UserThread.get().getUserId();
        UserInfo userInfo = new UserInfo();
        User user = userDao.getById(userId);
        BeanUtils.copyProperties(user, userInfo);
        return Result.success(userInfo);
    }

    @Override
    public Result status(UserStatus user) {
        String userId = UserThread.get().getUserId();
        User userInfo = userDao.getById(userId);
        AsserUtils.isFalse("2".equals(userInfo.getPermission()), BizCommonEnum.USER_NOT_PERMISSION_ERROR);
        userDao.setStatus(user.getId(), user.getStatus());
        return Result.success();
    }

    @Override
    public Result permission(UserPermission user) {
        String userId = UserThread.get().getUserId();
        User userInfo = userDao.getById(userId);
        AsserUtils.isFalse("2".equals(userInfo.getPermission()), BizCommonEnum.USER_NOT_PERMISSION_ERROR);
        userDao.setPermission(user.getId(), user.getPermission());
        return Result.success();
    }

    @Override
    public Result list(PageReq<User> req) {
        String userId = UserThread.get().getUserId();
        User userInfo = userDao.getById(userId);
        AsserUtils.isFalse("2".equals(userInfo.getPermission()), BizCommonEnum.USER_NOT_PERMISSION_ERROR);
        if (Objects.isNull(req)) {
            req = new PageReq<>();
        }
        IPage<User> page = userDao.list(req.getPage());
        List<UserInfo> list = page.getRecords().stream().map(item -> {
            UserInfo user = new UserInfo();
            BeanUtils.copyProperties(item, user);
            return user;
        }).collect(Collectors.toList());
        return PageResp.success(list, page.getTotal());
    }

    @Override
    public Result getById(String id) {
        AsserUtils.isEmpty(id,"id字段不能为空");
        User user = userDao.getById(id);
        UserInfo userInfo = new UserInfo();
        BeanUtils.copyProperties(user,userInfo);
        return Result.success(userInfo);
    }
}




